package com.nus.iss.ad.dbhelper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.nus.iss.ad.model.CollectionPoint;
import com.nus.iss.ad.model.Department;
import com.nus.iss.ad.model.DeptRequest;
import com.nus.iss.ad.model.DeptRequestDetails;
import com.nus.iss.ad.model.DisbursementListModel;
import com.nus.iss.ad.model.Inventory;
import com.nus.iss.ad.utils.JsonParser;
import com.nus.iss.ad.utils.MyConfig;

public class DisbursementDBHelper {

	static JSONArray json;

	public List<DisbursementListModel> getDisbursmentModelList() {

		String url = MyConfig.BACKEND_PATH + "/api/generateDisbursementList";
		// url=url+"/"+poId;

		if(json == null){
		json = JsonParser.getJSONArrayFromUrl(url);
		}

		List<DisbursementListModel> disMlist = new ArrayList<DisbursementListModel>();

		// CollectionPoint[] collectionPoints = null;

		DeptRequest[] deptRequests = null;

		// DeptRequestDetails[] deptRequestDetails = null;

		DisbursementListModel dml = null;

		try {
			JSONArray jsonOrderDetailArray = json;

			deptRequests = new DeptRequest[jsonOrderDetailArray.length()];

			for (int i = 0; i < jsonOrderDetailArray.length(); i++) {
				dml = new DisbursementListModel();
				JSONObject o = jsonOrderDetailArray.getJSONObject(i);

				// to collectionPoint
				CollectionPoint col = new CollectionPoint();
				JSONObject colj = o.getJSONObject("CollectionPoint");
				col.setCollectionId(colj.getInt("collectionPtId"));
				col.setDescription(colj.getString("Description"));

				// dml

				dml.setNumberofDept(o.getJSONArray("RequestList").length());

				// dml = new DisbursementListModel();

				dml.setCollectionPoint(col);
				// dml.setDeptRequest(Arrays.asList(deptRequests));

				disMlist.add(dml);

			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return disMlist;

	}

	public DisbursementListModel getDisbursementListModelforCollectionPt(
			int collId) throws JSONException {

		DisbursementListModel listModel = new DisbursementListModel();

		String url = MyConfig.BACKEND_PATH + "/api/generateDisbursementList";

		//json = JsonParser.getJSONArrayFromUrl(url);

		DeptRequest[] deptRequests = null;
		// DeptRequestDetails[] deptRequestDetails = null;

		List<DeptRequestDetails> depReqDetList = null;
		DeptRequest depR;
		DeptRequestDetails depReqDetail;

		for (int i = 0; i < json.length(); i++) {

			JSONObject colobj = json.getJSONObject(i);

			if (colobj.getJSONObject("CollectionPoint")
					.getInt("collectionPtId") == collId) {
				JSONArray deptReqArray = colobj.getJSONArray("RequestList");

				listModel.setCollectionPointName(colobj.getJSONObject(
						"CollectionPoint").getString("Description"));
				deptRequests = new DeptRequest[deptReqArray.length()];

				depReqDetList = new ArrayList<DeptRequestDetails>();

				for (int j = 0; j < deptReqArray.length(); j++) {
					depR = new DeptRequest();

					depR.setDepartmentName(deptReqArray.getJSONObject(j)
							.getJSONObject("department").getString("name"));

					depR.setContactPerson(deptReqArray.getJSONObject(j)
							.getJSONObject("department")
							.getString("contactPerson"));

					JSONArray depReqDetails = deptReqArray.getJSONObject(j)
							.getJSONArray("deptRequestDetails");

					depReqDetList = new ArrayList<DeptRequestDetails>();

					for (int k = 0; k < depReqDetails.length(); k++) {
						depReqDetail = new DeptRequestDetails();

						depReqDetail.setDeptReqId(depReqDetails
								.getJSONObject(k).getInt("id"));

						depReqDetail.setItemName(depReqDetails.getJSONObject(k)
								.getJSONObject("inventory")
								.getString("description"));
						depReqDetail.setQty(depReqDetails.getJSONObject(k)
								.getInt("qty"));
						depReqDetail.setQtyDelivered(depReqDetails
								.getJSONObject(k).getInt("qtyDelivered"));

						depReqDetList.add(depReqDetail);
					}

					depR.setDeptRequestDetails(depReqDetList);

					deptRequests[j] = depR;

				}

				listModel.setDeptRequest(Arrays.asList(deptRequests));
				System.out.println(listModel);

			}

		}

		return listModel;
	}

	public void confirmDeliveryDisbursment() {

		String url = MyConfig.BACKEND_PATH + "/api/confirmDelivery";

		JsonParser.postStream(url, json.toString());

	}

	public void updateJSON(int collectionPtID, int depReqID, int quantity)
			throws JSONException {

		for (int i = 0; i < json.length(); i++) {

			JSONObject level1 = json.getJSONObject(i);
			if (collectionPtID == level1.getJSONObject("CollectionPoint")
					.getInt("collectionPtId")) {

				for (int j = 0; j < level1.getJSONArray("RequestList").length(); j++) {

					JSONObject level2 = level1.getJSONArray("RequestList")
							.getJSONObject(j);

					for (int k = 0; k < level2.getJSONArray(
							"deptRequestDetails").length(); k++) {

						if (level2.getJSONArray("deptRequestDetails")
								.getJSONObject(k).getInt("id") == depReqID) {

							level2.getJSONArray("deptRequestDetails")
									.getJSONObject(k)
									.put("qtyDelivered", quantity);
							break;

						}

					}

				}

			}

		}

	}
	
	public static void clearJSON() {
		
		json = null;
		
	}



}
